package com.huifaapp.mall.selectImage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.huifaapp.mall.R;


/**
 * @author chen
 */
@SuppressLint("InflateParams")
public class ScreenMenu extends BottomBaseMenu {

    private Context context;
    private String title;
    private SelectAdapter adapter;
    private OnItemClickListener listener;
    private ListView mListView;

    public ScreenMenu(Context context) {
        super(context);
        this.context = context;
    }

    public SelectAdapter getAdapter() {
        return adapter;
    }

    /**
     * 设置菜单标题
     * @param title
     * @return
     */
    public ScreenMenu setTitle(String title) {
        //this.title = title;
        return this;
    }

    public void addItem(String item){
        adapter.addItem(item);
    }

    public void remove(int index){
        adapter.removeItem(index);
    }

    public int getItemCount(){
        return adapter.getItemCount();
    }

    /**
     * 设置适配器
     * @param adapter
     * @return
     */
    public ScreenMenu setAdapter(SelectAdapter adapter) {
        this.adapter = adapter;
        return this;
    }

    /**
     * 设置list监听
     * @param listener
     * @return
     */
    public ScreenMenu setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
        return this;
    }

    @Override
    protected View getView() {
        View view = LayoutInflater.from(context).inflate(R.layout.screen_layout, null);

        mListView = (ListView)view.findViewById(R.id.screenList);
        mListView.setAdapter(adapter);
        mListView.setDividerHeight(0);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                if (listener != null) {
                    listener.onItemClick(parent, view, position, id);
                }
            }
        });

        TextView textTitle = (TextView)view.findViewById(R.id.menuTitle);
        if (!TextUtils.isEmpty(title)) {
            textTitle.setText(title);
        } else {
            textTitle.setVisibility(View.GONE);
        }
        return view;
    }

    public ListView getListView(){
        return mListView;
    }

    @Override
    protected int getInAnimation() {
        return R.anim.roll_out;
    }

    @Override
    protected int getOutAnimation() {
        return R.anim.top_out;
    }
}
